// 通用类型定义

// 基础响应类型
export interface ApiResponse<T = any> {
  code: number;
  message: string;
  data: T;
  success: boolean;
}

// 分页请求参数
export interface PaginationParams {
  page: number;
  pageSize: number;
  total?: number;
}

// 分页响应数据
export interface PaginationResponse<T> {
  list: T[];
  total: number;
  page: number;
  pageSize: number;
  totalPages: number;
}

// 用户相关类型
export interface User {
  id: string;
  username: string;
  email: string;
  avatar?: string;
  phone?: string;
  role: 'admin' | 'trainer' | 'member';
  status: 'active' | 'inactive' | 'banned';
  createdAt: string;
  updatedAt: string;
  lastLoginAt?: string;
  profile?: UserProfile;
}

export interface UserProfile {
  firstName?: string;
  lastName?: string;
  gender?: 'male' | 'female' | 'other';
  birthday?: string;
  height?: number; // cm
  weight?: number; // kg
  bio?: string;
  preferences?: {
    language: string;
    theme: string;
    notifications: boolean;
  };
}

// 认证相关类型
export interface LoginRequest {
  email: string;
  password: string;
  rememberMe?: boolean;
}

export interface RegisterRequest {
  username: string;
  email: string;
  password: string;
  confirmPassword: string;
}

export interface AuthResponse {
  user: User;
  token: string;
  refreshToken: string;
  expiresIn: number;
}

export interface ResetPasswordRequest {
  email: string;
  token: string;
  newPassword: string;
}

// 会员相关类型
export interface Member {
  id: string;
  name: string;
  email: string;
  phone: string;
  avatar?: string;
  status: 'active' | 'inactive' | 'suspended';
  joinDate: string;
  lastActiveDate?: string;
  notes?: string;
  tags?: string[];
  personalInfo: {
    gender?: 'male' | 'female' | 'other';
    birthday?: string;
    height?: number;
    weight?: number;
    emergencyContact?: {
      name: string;
      phone: string;
      relationship: string;
    };
  };
  membershipInfo: {
    type: 'basic' | 'premium' | 'vip';
    startDate: string;
    endDate: string;
    isActive: boolean;
  };
  fitnessGoals?: string[];
  medicalConditions?: string[];
  createdAt: string;
  updatedAt: string;
}

export interface MemberCreateRequest {
  name: string;
  email: string;
  phone: string;
  avatar?: string;
  notes?: string;
  tags?: string[];
  personalInfo?: Partial<Member['personalInfo']>;
  membershipInfo: Member['membershipInfo'];
  fitnessGoals?: string[];
  medicalConditions?: string[];
}

export interface MemberUpdateRequest extends Partial<MemberCreateRequest> {
  id: string;
}

export interface MemberSearchParams extends PaginationParams {
  keyword?: string;
  status?: Member['status'];
  membershipType?: Member['membershipInfo']['type'];
  tags?: string[];
  sortBy?: 'name' | 'joinDate' | 'lastActiveDate';
  sortOrder?: 'asc' | 'desc';
}

// 动作库相关类型
export interface Exercise {
  id: string;
  name: string;
  description: string;
  category: ExerciseCategory;
  equipment: Equipment[];
  muscleGroups: MuscleGroup[];
  difficulty: 'beginner' | 'intermediate' | 'advanced';
  instructions: string[];
  tips?: string[];
  warnings?: string[];
  images?: string[];
  videos?: string[];
  duration?: number; // 秒
  calories?: number; // 每分钟消耗的卡路里
  tags?: string[];
  isPublic: boolean;
  createdBy: string;
  createdAt: string;
  updatedAt: string;
}

export interface ExerciseCategory {
  id: string;
  name: string;
  description?: string;
  icon?: string;
  color?: string;
}

export interface Equipment {
  id: string;
  name: string;
  description?: string;
  icon?: string;
  category?: string;
}

export interface MuscleGroup {
  id: string;
  name: string;
  description?: string;
  bodyPart: 'upper' | 'lower' | 'core' | 'full';
  icon?: string;
}

export interface ExerciseCreateRequest {
  name: string;
  description: string;
  categoryId: string;
  equipmentIds: string[];
  muscleGroupIds: string[];
  difficulty: Exercise['difficulty'];
  instructions: string[];
  tips?: string[];
  warnings?: string[];
  images?: string[];
  videos?: string[];
  duration?: number;
  calories?: number;
  tags?: string[];
  isPublic?: boolean;
}

export interface ExerciseUpdateRequest extends Partial<ExerciseCreateRequest> {
  id: string;
}

export interface ExerciseSearchParams extends PaginationParams {
  keyword?: string;
  categoryId?: string;
  equipmentIds?: string[];
  muscleGroupIds?: string[];
  difficulty?: Exercise['difficulty'];
  tags?: string[];
  isPublic?: boolean;
  createdBy?: string;
  sortBy?: 'name' | 'difficulty' | 'createdAt';
  sortOrder?: 'asc' | 'desc';
}

// 训练计划相关类型
export interface TrainingPlan {
  id: string;
  name: string;
  description?: string;
  memberId: string;
  trainerId: string;
  exercises: PlanExercise[];
  duration: number; // 分钟
  difficulty: 'beginner' | 'intermediate' | 'advanced';
  goals: string[];
  notes?: string;
  isTemplate: boolean;
  status: 'draft' | 'active' | 'completed' | 'archived';
  createdAt: string;
  updatedAt: string;
}

export interface PlanExercise {
  exerciseId: string;
  exercise?: Exercise;
  order: number;
  sets: ExerciseSet[];
  restTime?: number; // 秒
  notes?: string;
}

export interface ExerciseSet {
  id: string;
  reps?: number;
  weight?: number; // kg
  duration?: number; // 秒
  distance?: number; // 米
  restTime?: number; // 秒
  completed?: boolean;
  notes?: string;
}

export interface TrainingPlanCreateRequest {
  name: string;
  description?: string;
  memberId: string;
  exercises: Omit<PlanExercise, 'exercise'>[];
  goals: string[];
  notes?: string;
  isTemplate?: boolean;
}

export interface TrainingPlanUpdateRequest extends Partial<TrainingPlanCreateRequest> {
  id: string;
}

// 训练记录相关类型
export interface TrainingRecord {
  id: string;
  memberId: string;
  member?: Member;
  trainerId: string;
  trainer?: User;
  planId?: string;
  plan?: TrainingPlan;
  date: string;
  startTime: string;
  endTime?: string;
  duration?: number; // 分钟
  exercises: RecordExercise[];
  notes?: string;
  rating?: number; // 1-5
  feedback?: string;
  status: 'scheduled' | 'in_progress' | 'completed' | 'cancelled';
  createdAt: string;
  updatedAt: string;
}

export interface RecordExercise {
  exerciseId: string;
  exercise?: Exercise;
  order: number;
  sets: RecordSet[];
  notes?: string;
  completed: boolean;
}

export interface RecordSet {
  id: string;
  reps?: number;
  weight?: number;
  duration?: number;
  distance?: number;
  restTime?: number;
  completed: boolean;
  notes?: string;
}

export interface TrainingRecordCreateRequest {
  memberId: string;
  planId?: string;
  date: string;
  startTime: string;
  exercises: Omit<RecordExercise, 'exercise'>[];
  notes?: string;
}

export interface TrainingRecordUpdateRequest extends Partial<TrainingRecordCreateRequest> {
  id: string;
  endTime?: string;
  rating?: number;
  feedback?: string;
  status?: TrainingRecord['status'];
}

// 课程排期相关类型
export interface Schedule {
  id: string;
  title: string;
  description?: string;
  memberId: string;
  member?: Member;
  trainerId: string;
  trainer?: User;
  planId?: string;
  plan?: TrainingPlan;
  date: string;
  startTime: string;
  endTime: string;
  location?: string;
  type: 'personal' | 'group' | 'class';
  status: 'scheduled' | 'confirmed' | 'in_progress' | 'completed' | 'cancelled' | 'no_show';
  notes?: string;
  reminders?: {
    email: boolean;
    sms: boolean;
    push: boolean;
    time: number; // 提前多少分钟提醒
  };
  createdAt: string;
  updatedAt: string;
}

export interface ScheduleCreateRequest {
  title: string;
  description?: string;
  memberId: string;
  planId?: string;
  date: string;
  startTime: string;
  endTime: string;
  location?: string;
  type: Schedule['type'];
  notes?: string;
  reminders?: Schedule['reminders'];
}

export interface ScheduleUpdateRequest extends Partial<ScheduleCreateRequest> {
  id: string;
  status?: Schedule['status'];
}

export interface ScheduleSearchParams extends PaginationParams {
  memberId?: string;
  trainerId?: string;
  date?: string;
  startDate?: string;
  endDate?: string;
  status?: Schedule['status'];
  type?: Schedule['type'];
}

// 统计相关类型
export interface DashboardStats {
  members: {
    total: number;
    active: number;
    newThisMonth: number;
    growth: number; // 百分比
  };
  schedules: {
    today: number;
    thisWeek: number;
    thisMonth: number;
    upcoming: number;
  };
  exercises: {
    total: number;
    categories: number;
    popular: Exercise[];
  };
  revenue?: {
    thisMonth: number;
    lastMonth: number;
    growth: number;
  };
}

export interface MemberStats {
  totalSessions: number;
  completedSessions: number;
  totalDuration: number; // 分钟
  averageRating: number;
  favoriteExercises: Exercise[];
  progressData: {
    date: string;
    weight?: number;
    bodyFat?: number;
    muscle?: number;
  }[];
}

// 通知相关类型
export interface Notification {
  id: string;
  userId: string;
  type: 'info' | 'success' | 'warning' | 'error';
  title: string;
  message: string;
  data?: any;
  read: boolean;
  createdAt: string;
}

// 文件上传相关类型
export interface UploadFile {
  id: string;
  name: string;
  url: string;
  size: number;
  type: string;
  uploadedAt: string;
}

export interface UploadResponse {
  file: UploadFile;
  url: string;
}

// 应用设置相关类型
export interface AppSettings {
  theme: 'light' | 'dark' | 'auto';
  language: 'zh-CN' | 'en-US';
  primaryColor: string;
  notifications: {
    email: boolean;
    sms: boolean;
    push: boolean;
  };
  privacy: {
    showProfile: boolean;
    showStats: boolean;
    allowMessages: boolean;
  };
}

// 表单验证相关类型
export interface ValidationRule {
  required?: boolean;
  min?: number;
  max?: number;
  pattern?: RegExp;
  validator?: (value: any) => boolean | string;
  message?: string;
}

export interface FormField {
  name: string;
  label: string;
  type: 'text' | 'email' | 'password' | 'number' | 'select' | 'textarea' | 'date' | 'time';
  placeholder?: string;
  rules?: ValidationRule[];
  options?: { label: string; value: any }[];
  disabled?: boolean;
  readonly?: boolean;
}

// 错误处理相关类型
export interface AppError {
  code: string;
  message: string;
  details?: any;
  timestamp: string;
}

// 路由相关类型
export interface RouteConfig {
  path: string;
  name: string;
  component?: any;
  meta?: {
    title?: string;
    requiresAuth?: boolean;
    roles?: string[];
    keepAlive?: boolean;
  };
  children?: RouteConfig[];
}

// 主题相关类型
export interface ThemeColors {
  primary: string;
  secondary: string;
  success: string;
  warning: string;
  error: string;
  info: string;
  background: string;
  surface: string;
  text: {
    primary: string;
    secondary: string;
    disabled: string;
  };
}